<?php

namespace fankers\lib\qq;


use fankers\api\QqApi;
use fankers\config\QqConfig;
use fankers\lib\Base;

class Qq extends  Base
{
    public function __construct($params)
    {
        return parent::__construct($params);
    }

    ## come on 授权code
    public function start()
    {
        $oauth  = QqApi::OAUTH_URI;## 授权地址

        $params['client_id']  = QqConfig::APP_ID;
        $params['redirect_uri'] = $this->redirect;
        $params['response_type'] = QqConfig::RESPONSE_TYPE;
        $params['scope']  = QqConfig::SCOPE;
        $params['state']  = $this->randCharts(32);

        ## http_build_query 有urlencode的功能
        $url = $oauth."?".http_build_query($params);

        if (!isset($_GET['code'])) {
            header('Location:'.$url);
            exit();
        }
    }

    ## 获取 access_token
    public function token($params)
    {
        $data = [
            'client_id' => QqConfig::APP_ID,
            'client_secret' => QqConfig::APP_KEY,
            'grant_type' => QqConfig::GRANT_TYPE,
            'redirect_uri' => $this->redirect
        ];

        if (is_array($params)) {
            $data = array_merge($data,$params);
        }
        #@todo 兼容pc和wap
        $tokenUrl = QqApi::ACCESS_TOKEN_PC;#token pc端地址

        $tokenInfo = $this->http->get($tokenUrl,$data);
        return $this->http->parseQueryParams($tokenInfo);
    }

    ## 获取用户的openid 返回的client_id 和 openid
    /**callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} ); pc端返回的数据格式
     * 恶心的企鹅公司 pc和移动端返回的数据格式不一样
     * @param $params
     * @return array|mixed
     */
    public function openid($params)
    {
        $openidUrl = QqApi::OPEN_ID_PC;
        $openidInfo = $this->http->get($openidUrl,$params);
        return $this->http->message2array($openidInfo);
    }


    ## 获取用户信息
    public function info($params)
    {
        $infoUrl = qqApi::USER_INFO;

        $userInfo = $this->http->get($infoUrl,$params);

        return $this->http->parseJSON($userInfo);
    }

}